//
//  LYHTableViewHeaderView.m
//  微考
//
//  Created by BrianLiu on 2017/5/12.
//  Copyright © 2017年 BrianLiu. All rights reserved.
//

#import "LYHTableViewHeaderView.h"

@interface LYHTableViewHeaderView ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, weak) NSTimer* timer;

@end

@implementation LYHTableViewHeaderView

-(void)awakeFromNib
{
    [super awakeFromNib];
    // 设置代理
    self.scrollView.delegate = self;
    //开启分页
    self.scrollView.pagingEnabled = YES;
    //单页隐藏
    self.pageControl.hidesForSinglePage = YES;
    //设置pageControl的颜色
    self.pageControl.pageIndicatorTintColor = [UIColor redColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    //设置不显示滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    //设置页码图片
    //    [self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];
    //    [self.pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"];
    [self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];
    [self.pageControl setValue:[UIImage imageNamed:@"other"] forKeyPath:@"_pageImage"];
}

+(instancetype)showView
{
    return [[[NSBundle mainBundle]loadNibNamed:@"LYHTableViewHeaderView" owner:nil options:nil]lastObject];
}

-(void)setImageNames:(NSArray *)imageNames
{
    //创建图片控件,添加图片
    for (int i = 0; i < imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        CGFloat imageW =self.scrollView.frame.size.width;
        CGFloat imageH = self.scrollView.frame.size.height;
        //        NSString *imageName = [NSString stringWithFormat:@"wow%i",i+1];
        imageView.image = [UIImage imageNamed:imageNames[i]];
        imageView.frame = CGRectMake(i *imageW, 0, imageW, imageH);
        //将图片控件添加到scrollView
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.contentSize = CGSizeMake(imageNames.count *self.scrollView.frame.size.width, 0);
    //设置pageControl的数量
    self.pageControl.numberOfPages = imageNames.count;
    //开启定时器
    [self starTimer];
}


#pragma mark ----实现代理方法
//pageControl确定当前页
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = self.scrollView.contentOffset.x / self.scrollView.frame.size.width + 0.5;
    self.pageControl.currentPage = page;
}

#pragma mark ---- 定时器
-(void)nextPage
{
    NSUInteger page = self.pageControl.currentPage +1;
    if (page == 12) {
        page =0;
    }
    [self.scrollView setContentOffset:CGPointMake(page *self.scrollView.frame.size.width, 0) animated:YES];
    
}

-(void)starTimer
{
    NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    self.timer = timer;
}

-(void)stopTimer
{
    [self.timer invalidate];
}

//监听代理方法调用定时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self starTimer];
}



@end
